package Game;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.logging.Level;

import Levels.LevelLoader;
import LifeForms.Chaser;

public class Collision {
	boolean colli;
	public void objectCollision(ArrayList<Dude> characters,ArrayList<Entity> objects,int boundX,int boundY){
		for(int i=0;i<characters.size();i++){
			int collisions=0;
			Rectangle meXL=new Rectangle(characters.get(i).getX()-2,characters.get(i).getY(),1,characters.get(i).sprite.getHeight());
			Rectangle meXR=new Rectangle(characters.get(i).getX()+characters.get(i).sprite.getWidth(),characters.get(i).getY(),2,characters.get(i).sprite.getHeight());
			Rectangle meYU=new Rectangle(characters.get(i).getX(),characters.get(i).getY()-2,characters.get(i).sprite.getWidth(),2);
			Rectangle meYD=new Rectangle(characters.get(i).getX(),characters.get(i).getY()+characters.get(i).sprite.getHeight(),characters.get(i).sprite.getWidth(),2);
			for(Entity o:objects){
				if(meXL.intersects(o.getMe())&&characters.get(i).getHorMove()<0&&o.hardness>1){
					characters.get(i).setHorMove(0);
				}						
				if(meXR.intersects(o.getMe())&&characters.get(i).getHorMove()>0&&o.hardness>1){
					characters.get(i).setHorMove(0);
				}
				if(meYU.intersects(o.getMe())&&characters.get(i).getVerMove()<0&&o.hardness>1){
					characters.get(i).setVerMove(0);
				}
				if(meYD.intersects(o.getMe())&&characters.get(i).getVerMove()>0&&o.hardness>1){
					characters.get(i).setVerMove(0);
				}
				else if(characters.get(i).getMe().intersects(o.getMe())){
					collisions++;
				}
				//TODO make character to charater collision
			}
			if(!(collisions==0)){
				characters.get(i).setDelta(500);
			}else{
				characters.get(i).setDelta(1000);
			}
		}
	}
	public void giveCollision(ArrayList<Projectile> projectiles,ArrayList<Entity> objects,ArrayList<Chaser> enamies,int x,int y){
		outerloop:
			for (int i=0;i<projectiles.size();i++){
				Rectangle me=new Rectangle(projectiles.get(i).getX(),projectiles.get(i).getY(),projectiles.get(i).sprite.getWidth(),projectiles.get(i).sprite.getHeight());
				if(projectiles.get(i).getX()<x-10||projectiles.get(i).getX()>x+760){
					projectiles.remove(i);
					break;
				}
				if(projectiles.get(i).getY()<y-10||projectiles.get(i).getY()>y+410){
					projectiles.remove(i);
					break;
				}
				for(int i2=0;i2<enamies.size();i2++){
					if(me.intersects(enamies.get(i2).getMe())){
						enamies.remove(i2);
						projectiles.remove(i);
						break outerloop;
					}
				}
				for(int i2=0;i2<objects.size();i2++){
					if(me.intersects(objects.get(i2).getMe())&&objects.get(i2).hardness>1){	
					projectiles.remove(i);
					break outerloop;
				}
			}
		}
		for(int i=0;i<enamies.size();i++){
			Rectangle meXL=new Rectangle(enamies.get(i).getX()-2,enamies.get(i).getY(),1,enamies.get(i).sprite.getHeight());
			Rectangle meXR=new Rectangle(enamies.get(i).getX()+enamies.get(i).sprite.getWidth(),enamies.get(i).getY(),2,enamies.get(i).sprite.getHeight());
			Rectangle meYU=new Rectangle(enamies.get(i).getX(),enamies.get(i).getY()-2,enamies.get(i).sprite.getWidth(),2);
			Rectangle meYD=new Rectangle(enamies.get(i).getX(),enamies.get(i).getY()+enamies.get(i).sprite.getHeight(),enamies.get(i).sprite.getWidth(),2);
			for(Entity o:objects){
				if(meXL.intersects(o.getMe())&&enamies.get(i).getHorMove()<0&&o.hardness>1){
					enamies.get(i).setHorMove(0);
				}
				if(meXR.intersects(o.getMe())&&enamies.get(i).getHorMove()>0&&o.hardness>1){
					enamies.get(i).setHorMove(0);
				}
				if(meYU.intersects(o.getMe())&&enamies.get(i).getVerMove()<0&&o.hardness>1){
					enamies.get(i).setVerMove(0);
				}
				if(meYD.intersects(o.getMe())&&enamies.get(i).getVerMove()>0&&o.hardness>1){
					enamies.get(i).setVerMove(0);
				}
			}
		}
	}
	public void ChaserCollision(ArrayList<Dude> characters,ArrayList<Chaser> chasers){
		for(Dude d:characters){
			for(Chaser c:chasers){
				if(d.getMe().intersects(c.getMe())){
					d.doHealth(false,0.5);
				}
			}
		}
	}
	public void portalCollision(ArrayList<Dude> characters,ArrayList<Portal> portals){
		for(Dude d:characters){
			Rectangle me =new Rectangle(d.getX(),d.getY(),d.sprite.getWidth(),d.sprite.getHeight());
			int p=0;
			for(int i=0;i<portals.size();i++){
				if(i==0){
					p=0;
				}
				if(me.intersects(portals.get(i).getMe())&&!d.collision){
					portals.get(i).hit(d);
					d.collision=true;
					portals.get(i).collision=true;
					p++;
				}
				else if(me.intersects(portals.get(i).getMe())){
					p++;
				}
				else if(!me.intersects(portals.get(i).getMe()))
					portals.get(i).collision=false;
			}
			if(p==0){
				d.collision=false;
			}
		}
	}
}
